package org.example;

import cn.hserver.core.server.util.JsonResult;
import cn.hserver.plugin.web.annotation.Controller;
import cn.hserver.plugin.web.annotation.GET;
import cn.hserver.plugin.web.annotation.POST;
import cn.hserver.plugin.web.context.PartFile;
import cn.hserver.plugin.web.interfaces.HttpRequest;
import cn.hserver.plugin.web.interfaces.HttpResponse;
import cn.hserver.plugin.web.interfaces.ProgressStatus;

import java.io.File;
import java.util.Map;


@Controller
public class TestController {

    @POST("/upload")
    public JsonResult upload(HttpRequest request) {
        Map<String, PartFile> multipartFile = request.getMultipartFile();
        multipartFile.forEach((k, v) -> {
            System.out.println(v.getFile().getAbsolutePath());
            v.deleteTempCacheFile();
        });
        return JsonResult.ok();
    }

    @GET("/download")
    public void download(HttpRequest request, HttpResponse response) throws Exception {
        response.setDownloadBigFile(
                new File("/Users/heixiaoma/Downloads/DJI_20240324104129_0068_D.MP4"),
                new ProgressStatus() {
                    @Override
                    public void operationComplete(String s) {
                        System.out.println(s);
                    }
                    @Override
                    public void downloading(long l, long l1) {
                        System.out.println(l);
                    }
                },
                request
        );
    }

    @GET("/test")
    public JsonResult test(){
        return JsonResult.ok();
    }

}
